$.ajaxSetup({
    beforeSend:function(request){
        var token = sessionStorage.getItem("token");
        if(token){
            request.setRequestHeader("Authorization", "Bearer "+token);
        }
    },
    complete:function(xhr,status){
        var responseTxt = xhr.responseText;
        var data = JSON.parse(responseTxt);
        if(data.status==0){
            alert("未授权，请重新登陆");
            window.location.href = "/home.asp";
            return;
        }
         if(data.status==1){
            refreshToken();
            alert("请求失败，请重试");
        }
    }
});



function refreshToken(callback){
    var token = sessionStorage.getItem("refresh_token");
    $.ajax({
        url:"/refresh",
        type:"POST",
        dataType:"json",
        headers:{
            "Authorization": "Bearer "+token
        },
        success:function(response){
            sessionStorage.setItem("token",response.access_token);
            if(callback){
                callback();
            }
        }
    });
}